#!/bin/bash

# 快速数据库迁移脚本
# 只迁移最关键的几个表

echo "🚀 开始执行关键表的数据库迁移..."

# 数据库连接参数
HOST="43.143.253.188"
USERNAME="nextjs"
PASSWORD="jack666"
DATABASE="nextjs"

echo "📋 数据库连接信息："
echo "   主机: $HOST"
echo "   用户: $USERNAME"
echo "   数据库: $DATABASE"
echo ""

# 确认执行
read -p "确认执行迁移？(y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo "❌ 迁移已取消"
    exit 1
fi

echo "🔄 正在执行关键表迁移..."

# 执行关键表的迁移
mysql -h $HOST -u $USERNAME -p$PASSWORD $DATABASE << EOF
-- 修改角色资源表（最关键的表）
ALTER TABLE \`character_resources\` CHANGE COLUMN \`createdAt\` \`created_at\` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间';
ALTER TABLE \`character_resources\` CHANGE COLUMN \`updatedAt\` \`updated_at\` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间';

-- 修改用户表
ALTER TABLE \`users\` CHANGE COLUMN \`createdAt\` \`created_at\` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间';
ALTER TABLE \`users\` CHANGE COLUMN \`updatedAt\` \`updated_at\` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间';

-- 修改角色表
ALTER TABLE \`characters\` CHANGE COLUMN \`createdAt\` \`created_at\` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间';
ALTER TABLE \`characters\` CHANGE COLUMN \`updatedAt\` \`updated_at\` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间';

-- 修改Casino相关表
ALTER TABLE \`casino_dice_rounds\` CHANGE COLUMN \`createdAt\` \`created_at\` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间';
ALTER TABLE \`casino_dice_rounds\` CHANGE COLUMN \`updatedAt\` \`updated_at\` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间';

ALTER TABLE \`casino_chibi_rounds\` CHANGE COLUMN \`createdAt\` \`created_at\` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间';
ALTER TABLE \`casino_chibi_rounds\` CHANGE COLUMN \`updatedAt\` \`updated_at\` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间';

ALTER TABLE \`casino_dice_bets\` CHANGE COLUMN \`createdAt\` \`created_at\` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间';
ALTER TABLE \`casino_dice_bets\` CHANGE COLUMN \`updatedAt\` \`updated_at\` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间';

ALTER TABLE \`casino_chibi_bets\` CHANGE COLUMN \`createdAt\` \`created_at\` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间';
ALTER TABLE \`casino_chibi_bets\` CHANGE COLUMN \`updatedAt\` \`updated_at\` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间';

ALTER TABLE \`casino_rock_paper_scissors\` CHANGE COLUMN \`createdAt\` \`created_at\` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间';
ALTER TABLE \`casino_rock_paper_scissors\` CHANGE COLUMN \`updatedAt\` \`updated_at\` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间';

ALTER TABLE \`casino_rankings\` CHANGE COLUMN \`updatedAt\` \`updated_at\` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间';

ALTER TABLE \`casino_configs\` CHANGE COLUMN \`createdAt\` \`created_at\` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间';
ALTER TABLE \`casino_configs\` CHANGE COLUMN \`updatedAt\` \`updated_at\` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间';

ALTER TABLE \`casino_operation_logs\` CHANGE COLUMN \`createdAt\` \`created_at\` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间';
ALTER TABLE \`casino_operation_logs\` CHANGE COLUMN \`updatedAt\` \`updated_at\` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间';
EOF

if [ $? -eq 0 ]; then
    echo "✅ 关键表迁移执行成功！"
    echo ""
    echo "📝 接下来需要执行以下步骤："
    echo "1. 重启应用程序"
    echo "2. 测试赤壁下注功能"
else
    echo "❌ 关键表迁移执行失败！"
    exit 1
fi
